#include <iostream>
#include <vector>
using namespace std;

int combin(vector<int> &stons, vector<int> &sum, int i, int j)
{
        if(i == j){
                return 0;
        }

        if(i + 1 == j){
                return stons[i] + stons[j];
        }

        int val = INT32_MAX;
        for(int k = i; k < j; k++){
                int com = combin(stons, sum, i, k) + combin(stons, sum, k + 1, j) + sum[j] - sum[i] + stons[i];
                val = min(val, com);
        }

        return val;
}

int combin(vector<int> &stons, vector<int> &sum, int i, int j, vector<vector<int>> &recd)
{
        if(recd[i][j] != -1){
                return recd[i][j];
        }

        if(i == j){
                return 0;
        }

        if(i + 1 == j){
                return stons[i] + stons[j];
        }

        int val = INT32_MAX;
        for(int k = i; k < j; k++){
                int com = combin(stons, sum, i, k, recd) + combin(stons, sum, k + 1, j, recd) + sum[j] - sum[i] + stons[i];
                val = min(val, com);
        }

        return recd[i][j] = val;
}

int main()
{
        int n;
        cin>>n;
        vector<int> stons(n, 0);
        vector<int> sum(n, 0);
        vector<vector<int>> recd(n, vector<int>(n, -1));

        cin>>stons[0];
        sum[0] = stons[0];
        for(int i = 1; i < n; i++){
                cin>>stons[i];
                sum[i] = stons[i] + sum[i - 1];
        }

        cout<<combin(stons, sum, 0, n - 1, recd)<<endl;
        return 0;
}